package com.xl.bigdata.es.index;

import com.xl.bigdata.es.xpack.ESXpackRestHighLevelClient;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @ClassName IndexAPI
 * @Description TODO
 * @Author xl.gao
 * @Date 2021/12/30 10:06
 * @Version
 */
public class IndexAPI {

    /**
     * @Description: 默认单条写入数据
     * @Author: xl.gao
     * @Date:
     * @param client:
     * @param index:
     * @param id:
     * @param json:
     * @return: boolean
     **/
    public static boolean indexJsonAPI(RestHighLevelClient client, String index, String id, String json){

        IndexRequest indexRequest = new IndexRequest();
        indexRequest.index(index);
        indexRequest.id(id);
        indexRequest.source(json, XContentType.JSON);

        try {
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

            ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
            if (shardInfo.getFailed() > 0) {
                for (ReplicationResponse.ShardInfo.Failure failure :
                        shardInfo.getFailures()) {
                    String reason = failure.reason();
                    System.out.println(reason);
                }
            } else {

                System.out.println("写入成功！");

                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;


    }

    /**
     * @Description:默认单条写入数据，立即可见
     * @Author: xl.gao
     * @Date:
     * @param client:
     * @param index:
     * @param id:
     * @param json:
     * @param refresh: true：立即可见，false：等待系统refresh以后可见
     * @return: boolean
     **/
    public static boolean indexJsonAPI(RestHighLevelClient client, String index, String id, String json, boolean refresh){

        IndexRequest indexRequest = new IndexRequest();
        indexRequest.index(index);
        indexRequest.id(id);
        indexRequest.source(json, XContentType.JSON);

        // 索引是否立即可见
        if(refresh){
            indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        }

        try {
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

            ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
            if (shardInfo.getFailed() > 0) {
                for (ReplicationResponse.ShardInfo.Failure failure :
                        shardInfo.getFailures()) {
                    String reason = failure.reason();
                    System.out.println(reason);
                }
            } else {

                System.out.println("写入成功！");

                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * @Description:
     * @Author: xl.gao
     * @Date:
     * @param args:
     * @return: void
     **/
    public static void main(String[] args) {
        RestHighLevelClient restHighLevelClient = ESXpackRestHighLevelClient.getRestHighLevelClient();

        indexJsonAPI(restHighLevelClient, "address_base", "test_index_api_02", "{\"citycomple\":\" 青岛01\"}", true);

        ESXpackRestHighLevelClient.closeRestHighLevelClient();
    }
}
